package com.burgeon.backend.SHOPBACKEND.rest.rfm;

import com.burgeon.backend.Rest.base.RESTBase;
import com.burgeon.backend.SHOPBACKEND.entity.rfm.RfmItemVO;
import com.burgeon.backend.SHOPBACKEND.entity.rfm.RfmVO;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;


public class QuerryRFMData extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public QuerryRFMData(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }


    /**
     * 发送并接收字符创
     * @return
     */

    public HashMap getData(String type) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";
        String str = RESULT(Data(type), url);

        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
            throw new Exception(str);
        }

        String count = new JSONArray(str).getJSONObject(0).getString("count");
        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        HashMap data = new HashMap();
        ArrayList<RfmItemVO> rfmItemVOS = new ArrayList<RfmItemVO>();

        for (int i = 0; i < jsonArray.length(); i++) {
            JSONArray array = jsonArray.getJSONArray(i);
            RfmItemVO rfmItemVO = new RfmItemVO();
            rfmItemVO.setId(array.getString(0));
            rfmItemVO.setName(array.getString(1));
            rfmItemVO.setValue(array.getInt(2));
            rfmItemVO.setVipRate(array.getDouble(3));
            rfmItemVO.setExpenditure(array.getDouble(4));
            rfmItemVO.setExpenditureRate(array.getDouble(5));
            rfmItemVO.setAverage(array.getDouble(6));
            rfmItemVOS.add(rfmItemVO);
        }
        JSONArray array = jsonArray.getJSONArray(0);
        RfmVO rfmVO = new RfmVO();
        rfmVO.setTitle(array.getString(7));
        rfmVO.setHeadcount(array.getString(8));
        rfmVO.setAlive(array.getDouble(9));
        rfmVO.setSleep(array.getDouble(10));
        rfmVO.setLose(array.getDouble(11));

        data.put("main",rfmVO);
        data.put("list",rfmItemVOS);
        data.put("count",count);

        return data;
    }

    private MultiValueMap Data(String type) {
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段

        columns.add("ID");
        columns.add("DESCRIPTION");//描述
        columns.add("VIP_QTY");//会员数
        columns.add("VIP_QTYRATIO");//会员占比
        columns.add("VIP_AMT");//消费金额
        columns.add("VIP_AMTRATIO");//消费金额占比
        columns.add("AVG_KDJ");//平均客单价

        columns.add("'YC_LABELDEFINE_ID;NAME'");//标题
        columns.add("'YC_LABELDEFINE_ID;TOT_QTY'");//总人数
        columns.add("'YC_LABELDEFINE_ID;ACTIVE_VIP'");//活跃
        columns.add("'YC_LABELDEFINE_ID;SLEEP_VIP'");//沉睡
        columns.add("'YC_LABELDEFINE_ID;LOSE_VIP'");//流失



        params.put("columns", columns);
        params.put("table", "YC_LABELDEFINEITEM");//表名

        params.put("count","true");

        HashMap paramss = new HashMap();//DOCNO  (AK)	单据编号ISACTIVE
        HashMap expr1 = new HashMap();
        String ac = "'="+type+"'";
        expr1.put("column", "'YC_LABELDEFINE_ID;NO'");//11665
        expr1.put("condition", ac);

        HashMap expr2 = new HashMap();
        String is = "'=Y'";
        expr2.put("column","ISENABLE");
        expr2.put("condition",is);

        paramss.put("combine", "and");//关系
        paramss.put("expr1",expr1);
        paramss.put("expr2",expr2);

        params.put("params", paramss);



        //System.err.println(params);

        return DATA("Query", params,account,password);
    }

    public static void main(String[] args) {
        QuerryRFMData querryYCRetailout = new QuerryRFMData("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
        try {
            System.err.println(querryYCRetailout.getData("7"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
